package com.bsg.upm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.annotation.Auth;
import com.bsg.upm.constant.UserRoleConstants;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.OrderService;

/**
 * 工单管理控制层
 * 
 * @author HCK
 *
 */
@Controller
@RequestMapping("/v1.0/orders")
public class OrderController {

	@Resource
	private OrderService orderService;

	/**
	 * 【查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listOrder(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			if (paramMap.containsKey("site")) {
				searchParamMap.put("siteId", paramMap.get("site"));
			}
			if (paramMap.containsKey("serv_type")) {
				searchParamMap.put("definitionServCode", paramMap.get("serv_type"));
			}
			if (paramMap.containsKey("type")) {
				searchParamMap.put("type", paramMap.get("type"));
			}
			if (paramMap.containsKey("status")) {
				searchParamMap.put("status", paramMap.get("status"));
			}
			respJson = orderService.list(searchParamMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【查询单条记录】
	 * 
	 * @param orderId
	 *            工单编码
	 * @return 操作结果
	 */
	@RequestMapping(value = "/{order_id}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getOrder(@PathVariable("order_id") String orderId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = orderService.get(orderId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【克隆】
	 * 
	 * @param orderId
	 *            工单编码
	 * @param cnt
	 *            克隆数量
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{order_id}/clone", method = RequestMethod.POST)
	@ResponseBody
	public RespJson cloneOrder(@PathVariable("order_id") String orderId,
			@RequestParam(value = "cnt", required = false) Integer cnt, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			if (cnt == null) {
				cnt = 1;
			}
			respJson = orderService.clone(orderId, cnt);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【新增】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return 操作结果
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public RespJson saveOrder(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = orderService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【编辑】
	 * 
	 * @param orderId
	 *            工单编码
	 * @param paramMap
	 *            请求参数
	 * @return 操作结果
	 */
	@RequestMapping(value = "/{order_id}", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateOrder(@PathVariable("order_id") String orderId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("orderId", orderId);
			respJson = orderService.update(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【审批】
	 * 
	 * @param orderId
	 *            工单编码
	 * @param paramMap
	 *            请求参数
	 * @return RespJson请求结果对象
	 */
	@Auth(value = { UserRoleConstants.ADMIN })
	@RequestMapping(value = "/{order_id}/approve", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson approveOrder(@PathVariable("order_id") String orderId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("orderId", orderId);
			respJson = orderService.approve(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【执行】
	 * 
	 * @param orderId
	 *            工单编码
	 * @param paramMap
	 *            请求参数
	 * @return 操作结果
	 */
	@Auth(value = { UserRoleConstants.ADMIN })
	@RequestMapping(value = "/{order_id}/execution", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson executeOrder(@PathVariable("order_id") String orderId,
			@RequestBody(required = false) Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			if (paramMap == null) {
				paramMap = new HashMap<String, Object>();
			}
			paramMap.put("orderId", orderId);
			respJson = orderService.execute(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【删除】
	 * 
	 * @param orderId
	 *            工单编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{orderId}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeOrder(@PathVariable("orderId") String orderId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = orderService.remove(orderId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
}
